#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>

using namespace std;

__thread int gval = 100;

string ToHex(pthread_t tid)
{
    char id[128];
    snprintf(id, sizeof(id), "0x%lx", tid);
    return id;
}

void *threadrun(void *args)
{
    string name = static_cast<const char *>(args);
    while (true)
    {
        string id = ToHex(pthread_self());
        cout << name << " is running,tid: " << id << ",gval: " << gval << ",&gval: " << &gval << endl;
        sleep(1);
        gval++;
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadrun, (void *)"thread-1");
    while (true)
    {
        cout << "main thread,gval: " << gval << ",&gval: " << &gval << endl;
        sleep(1);
    }
    pthread_join(tid, nullptr);

    return 0;
}